package com.wjk.cms.service.impl;


import com.wjk.cms.dao.UserRepository;
import com.wjk.cms.pojo.User;

import com.wjk.cms.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;



    @Override
    public User getByPhone( String phone) {

        if (StringUtils.isEmpty(phone)) {
            return null;
        }
        return userRepository.getByPhone( phone);
    }

    @Override
    public User getByUnionid( String unionid) {

        if (StringUtils.isEmpty(unionid)) {
            return null;
        }
        return userRepository.getByUnionid( unionid);
    }


    @Override
    public List<User> findAll() {
        return userRepository.findAll();
    }

    @Override
    public User findById(Long id) {
        return userRepository.findById(id).orElse(null);
    }

    @Override
    public void save(User bean) {
        userRepository.save(bean);
    }

    @Override
    public List<User> findAll(String keyword, Integer pageNo, Integer pageSize) {
        if(pageNo==null || pageNo<1){
            return userRepository.findAll((root, query, cb) -> {
                List<Predicate> predicates = new ArrayList<>();


                if (StringUtils.isNotEmpty(keyword)) {
                    predicates.add(cb.or(
                            cb.like(root.get("phone"), "%" + keyword + "%"),

                            cb.like(root.get("nickname"), "%" + keyword + "%"),
                            cb.like(root.get("name"), "%" + keyword + "%")
                            )
                    );
                }
                return cb.and(predicates.toArray(new Predicate[0]));
            }, Sort.by(Sort.Order.desc("createTime")));
        }else {
            return userRepository.findAll((root, query, cb) -> {
                List<Predicate> predicates = new ArrayList<>();


                if (StringUtils.isNotEmpty(keyword)) {
                    predicates.add(cb.or(
                            cb.like(root.get("phone"), "%" + keyword + "%"),
                            cb.like(root.get("nickname"), "%" + keyword + "%"),
                            cb.like(root.get("name"), "%" + keyword + "%")
                            )
                    );
                }
                return cb.and(predicates.toArray(new Predicate[0]));
            }, PageRequest.of(pageNo - 1, pageSize, Sort.Direction.DESC, "createTime")).getContent();
        }
    }




    @Override
    public List<User> findChildren(Long userId,String keywords, Integer pageNo, Integer pageSize) {
        if(pageNo==null || pageNo<1){
            return userRepository.findAll((root, query, cb) -> {
                List<Predicate> predicates = new ArrayList<>();
                //不查询 未通过审核的
                predicates.add(cb.notEqual(root.get("status"),"2"));
                if (userId!=null && userId>0) {
                    predicates.add(cb.equal(root.get("parentId"),userId));

                }

                if(StringUtils.isNotBlank(keywords)){
                    predicates.add(cb.like(root.get("name"),"%"+keywords+"%"));
                }






                return cb.and(predicates.toArray(new Predicate[0]));
            }, Sort.by(Sort.Order.desc("createTime")));
        }else {
            return userRepository.findAll((root, query, cb) -> {
                List<Predicate> predicates = new ArrayList<>();

                predicates.add(cb.notEqual(root.get("status"),"2"));
                if (userId!=null && userId>0) {
                    predicates.add(cb.equal(root.get("parentId"),userId));

                }
                if(StringUtils.isNotBlank(keywords)){
                    predicates.add(cb.like(root.get("name"),"%"+keywords+"%"));
                }


                return cb.and(predicates.toArray(new Predicate[0]));
            }, PageRequest.of(pageNo - 1, pageSize, Sort.Direction.DESC, "createTime")).getContent();
        }
    }



    @Override
    public User getByWxXcxOpenid(String openid) {
        return userRepository.getByWxXcxOpenid(openid);
    }

    @Override
    public User getByTypeAndWxXcxOpenid(String type, String openid) {
        return userRepository.getByTypeAndWxXcxOpenid(type,openid);
    }


}
